﻿package	birdry.net	{
	
	import flash.display.BitmapData;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.net.URLVariables;
	import flash.net.URLRequestMethod;
	import flash.net.URLRequestHeader;
	import flash.net.URLLoaderDataFormat;
	import flash.utils.ByteArray;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.system.System;
	
	import birdry.net.encoder.JPGEncoder;
	import birdry.net.encoder.PNGEncoder;
	import birdry.net.UploadPostHelper;
	
	/*	
	ex)
	var bitmapUpload:BitmapUpload = new BitmapUpload();
	bitmapUpload.bitmapData = bitmapData;
	bitmapUpload.url = "http://lostsin.cafe24.com/upload.jsp";
	bitmapUpload.bitmapEncoder = "JPG";
	bitmapUpload.encoding = BitmapUpload.ENCODING_EUC_KR;
	bitmapUpload.upload({aa:"abcd", bb:"efg"});
	bitmapUpload.addEventListener(BitmapUpload_Event.SUCCESS, onUploadSuccess);
	private function onUploadSuccess( e: BitmapUpload_Event ): void	{
		trace( XML(e.data).returnValue );
	}
	*/
	
	public class BitmapUpload extends EventDispatcher 	{
		
		public static const ENCODING_EUC_KR: String = "euc_kr";
		public static const ENCODING_UTF_8: String = "utf-8";
		
		private var _parameters:URLVariables = new URLVariables();
		private var _urlRequest:URLRequest = new URLRequest();
		private var _urlLoader:URLLoader = new URLLoader();
		private var _byteArray:ByteArray;
		private var _encoding: String = "";
		private var _callBackComplete:Function;
		private var _bitmapEncoder:String;
		
		public var data:Object;
		
		public function get bitmapEncoder(): String	{
			return _bitmapEncoder;
		}
		public function set bitmapEncoder(value:String): void	{
			_bitmapEncoder = value;
		}
		
		public function BitmapUpload(): void	{
			this._bitmapEncoder = "jpg";
			this._encoding = BitmapUpload.ENCODING_UTF_8;
		}
		
		public function set encoding( value: String ): void	{
			if( value.toLocaleLowerCase() == XMLLoader.ENCODING_EUC_KR)	{
				this._encoding = value;
				System.useCodePage = true;
			}	 else if	( value.toLowerCase() == XMLLoader.ENCODING_UTF_8)	{
				this._encoding = value;
				System.useCodePage = false;
			}	else	{
				throw new Error("그런 인코딩 방식은 지원하지 않는다");
			}
		}
		public function upload(url:String, bitmapData:BitmapData, params: Object = null, callBackComplete:Function = null): void	{
			_callBackComplete = callBackComplete;
			
			createByteArray(bitmapData);
			
			for( var item: String in params)	{
				this._parameters[item] = params[item];
			}
			
			trace("------------		<< 이미지 업로드 실행 >>		-------------");
			trace("bitmapData => "+ bitmapData);
			trace("bitmap encoder => "+ this._bitmapEncoder);
			trace("upload Url => "+ url);
			trace("fileName => " + fileName);
			trace("encoding => " + this._encoding);
			trace("send Parameters => " + this._parameters);
			trace("-----------------------------------------------------------------");
			
			_urlRequest.url = url;
			//	_urlRequest.contentType = 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary();
			_urlRequest.method = URLRequestMethod.POST;
			_urlRequest.data = UploadPostHelper.getPostData(fileName, _byteArray, _parameters);
			_urlRequest.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );
			_urlRequest.requestHeaders.push( new URLRequestHeader( 'Content-Type', 'multipart/form-data; boundary=' +UploadPostHelper.getBoundary()) );
			
			_urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
			_urlLoader.addEventListener(Event.COMPLETE, onComplete);
			_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			_urlLoader.load(_urlRequest);
		}
		
		private function createByteArray( bitmapData:BitmapData ): void	{
			if(_bitmapEncoder == "jpg")		_byteArray = new JPGEncoder().encode(bitmapData);
			if(_bitmapEncoder == "png")		_byteArray = PNGEncoder.encode(bitmapData);		// 테스트 못함..ㅡㅡ;;
		}
		
		
		private function onComplete( e: Event ): void	{
			data = this._urlLoader.data;
			if (_callBackComplete != null)	_callBackComplete(data);
		}
		
		// IO Error 발생
		private function onIOError( e: Event ): void	{
			//this.dispatchEvent( new Event(BitmapUpload.IO_FAULT));
		}
		
		private function get fileName(): String	{
			return new Date().getTime().toString() + "." + _bitmapEncoder;
		}
		
	}	// class
}	// package

			
/*
	<?php
		$str_email = $_POST['str_email'];
		$str_pass = $_POST['str_pass'];
		$str_name = $_POST['str_name'];
		$str_birth = $_POST['str_birth'];
		$str_tel = $_POST['str_tel'];
		$str_memo = $_POST['str_memo'];
		
		$file_temp = $_FILES['Filedata']['tmp_name'];
		$file_name = $_FILES['Filedata']['name'];
		$file_size = $_FILES['Filedata']['size'];
		
		
		$movePath = "/home/myPath/images/$str_email_$file_name";
		
		move_uploaded_file($file_temp, $movePath);
	?>
	
	리턴은 xml 포멧으로
*/